const express = require('express');
const router = express.Router();

// 实体类
const Response = require('../entity/http/Response');
const Organization = require('../entity/job/Organization');
const ObjId = require('../entity/common/ObjId');
const Job = require('../entity/job/Job');
const CollectJob = require('../entity/job/CollectJob');

// 业务层
const hireService = require('../service/hireService');

/**
 * @接口名称 获取所有岗位类型接口
 */
router.get('/job-types', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /job-types 接口`);
    hireService.getAllJobTypes().then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

// /**
//  * @接口名称 创建一个组织接口
//  * @用途 用户需要创建一个组织，然后才可以发布招聘信息
//  * @说明 组织可以授权给任意一个用户使用，用于发布招聘信息
//  */
// router.post('/organization', async function(req, res, next) {
//     let uid = req.jwtResult.data.uid;
//     let organization = new Organization(req.body);
//     let flag = organization.check();
//     if (!flag.success) {
//         return res.status(400).json(flag);
//     }
//     // 调用业务层
//     hireService.createOrganization(uid, organization).then((result) => {
//         res.status(result.code).json(result);
//     }).catch((err) => {
//         res.status(500).json((new Response()).fail(500, "资源不可访问"));
//     });
// });

// /**
//  * @接口名称 发布岗位接口
//  * @用途 组织管理者可以发布岗位
//  */
// router.post('/job', async function(req, res, next) {
//     let uid = req.jwtResult.data.uid;
//     let job = new Job(req.body);
//     let flag = job.check();
//     if (!flag.success) {
//         return res.status(400).json(flag);
//     }
//     // 调用业务层
//     hireService.publishJob(uid, job).then((result) => {
//         res.status(result.code).json(result);
//     }).catch((err) => {
//         res.status(500).json((new Response()).fail(500, "资源不可访问"));
//     });
// });

/**
 * @接口名称 懒加载推荐岗位接口
 * @用途 用于招聘页面获取推荐岗位
 */
router.get('/recommend-jobs', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /recommend-jobs 接口`);
    let uid = req.jwtResult.data.uid;
    let job = new ObjId(req.query, 'lastId', true);
    let flag = job.check();
    if (!flag.success) {
        res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    hireService.getRecommendJobs(uid, job.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 获取岗位详情接口
 */
router.get('/job', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /job 接口`);
    let uid = req.jwtResult.data.uid;
    let job = new ObjId(req.query, 'id');
    let flag = job.check();
    if (!flag.success) {
        res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    hireService.getJobInfo(uid, job.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 收藏或取消收藏岗位接口
 * @参数详情 collect 为 true 表示收藏，为 false 表示取消收藏
 */
router.post('/collect-job', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /collect-job 接口`);
    let uid = req.jwtResult.data.uid;
    let collectJob = new CollectJob(req.body);
    let flag = collectJob.check();
    if (!flag.success) {
        res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    hireService.collectJob(uid, collectJob.id, collectJob.collect).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

/**
 * @接口名称 投递岗位接口
 */
router.post('/submit', async function(req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /submit 接口`);
    let uid = req.jwtResult.data.uid;
    let job = new ObjId(req.body, 'id');
    let flag = job.check();
    if (!flag.success) {
        res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    hireService.submit(uid, job.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail(500, "资源不可访问"));
    });
});

module.exports = router;
